<?php
/*
	Copyright 2008 Michael Stepanov (http://stepanoff.org)
	This program is distributed under the terms of the GNU General Public License
	
	This file is part of OWW Web Client.

    OWW Web Client is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OWW Web Client is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with OWW Web Client.
	If not, see <http://www.gnu.org/licenses/>.

*/
	require_once('includes/Smarty/libs/Smarty.class.php');
	require_once('includes/xajax/xajax_core/xajax.inc.php');

	$smarty_folder = 'smarty/';

#	$weather_data = array(
#		'Temperature' => array('value' => '24.4', 'unit_of_measure' => '&deg;C'),
#		'Wind Speed' => array('value' => '6', 'unit_of_measure' => 'm/c'),
#		'Wind direction' => array('value' => 'NE', 'unit_of_measure' => ''),
#		'Altimeter' => array('value' => '120', 'unit_of_measure' => 'mm'),
#		'Humidity' => array('value' => '76', 'unit_of_measure' => '%'),
#	);

	$data_map = array(
		'tm' 	=> array('caption' => 'Date Update', 'unit_of_measure' => 'time'),
		't1' 	=> array('caption' => 'Temperature', 'unit_of_measure' => '&deg;C'),
		'dp' 	=> array('caption' => 'Dew Point', 'unit_of_measure' => '&deg;C'),
		'hr' 	=> array('caption' => 'Relative Humidity', 'unit_of_measure' => '%'),
		'br' 	=> array('caption' => 'Altimeter', 'unit_of_measure' => 'MPH'),
		'wdspd' => array('caption' => 'Wind Speed', 'unit_of_measure' => 'KT'),
		'gst' 	=> array('caption' => 'Wind Gust', 'unit_of_measure' => ''),
		'wddir' => array('caption' => 'Wind Direction', 'unit_of_measure' => ''),
		'rn' 	=> array('caption' => 'Rain Gauge', 'unit_of_measure' => ''),
		'drn' 	=> array('caption' => 'Rain Gauge (Daily)', 'unit_of_measure' => ''),
		'rnrt' 	=> array('caption' => 'Rain Gauge (Rate)', 'unit_of_measure' => ''),
	);
	
	$db_host = 'localhost';
	$db_name = 'weather-info';	
	$db_user = 'root';
	$db_pass = '';
	$weather_table = 'weather_info';

	$weather_data = get_weather_data();

	$smarty = new Smarty();
	$smarty->template_dir 	= $smarty_folder.'templates';
	$smarty->compile_dir 	= $smarty_folder.'templates_c';
	$smarty->cache_dir 		= $smarty_folder.'cache';
	$smarty->config_dir 	= $smarty_folder.'configs';

	$xajax = new xajax();
	$xajax->setFlag('debug', false);
	$xajax->setRequestURI("index.php");
	$xajax->registerFunction("refreshData");
	$xajax->processRequest();

	$app_name 		= "OWW Web Client";
	$app_version 	= "0.01";
	$smarty->assign('app_name', $app_name);
	$smarty->assign('app_version', $app_version);

	$smarty->assign('xajax_js', $xajax->getJavascript('includes/xajax/'));
	$smarty->display('main.tpl');

function refreshData () {
	global $smarty, $weather_data;

	$objResponse = new xajaxResponse();	

	$date = array_shift($weather_data);

	$smarty->assign('weather_data', $weather_data);
	$objResponse->assign('content', 'innerHTML', $smarty->fetch('weather_info.tpl'));
	$objResponse->assign("date_time", "innerHTML", $date['value']);
	$objResponse->script('setTimeout("xajax_refreshData()", 2000);');
	return $objResponse;
}

function get_weather_data () {
	global $db_host, $db_name, $db_user, $db_pass, $weather_table;

	$con = mysql_connect($db_host, $db_user, $db_pass) or die('Could not connect: ' . mysql_error());
	mysql_select_db($db_name, $con);

	set_log_message("Connection to $db_name is OK");

	$result = mysql_query("SELECT tm, t1, dp, rh, bp,wdspd, gst, wddir, rn, drn, rnrt FROM $weather_table ORDER BY tm DESC LIMIT 1");

	$row = mysql_fetch_array($result);

	set_log_message("Getting data from DB");
	
	mysql_close($con);

	return process_data($row);
}

function set_log_message ($message, $level='DEBUG') {
	global $log_file;
	
	$message = ' [' . $level . '] ' . $message; 

	error_log($message, 0);
}

function process_data($data) {
	global $data_map;
	
	$results = array();

	foreach ($data as $key => $val) {
		if(!empty($data_map[$key])) {
			$caption = $data_map[$key]['caption'];

			$results[$caption] = array('value' => $val, 'unit_of_measure' => $data_map[$key]['unit_of_measure']);	
		}	
	}

	return $results;
}

?>
